package com.novax.ex.activity.provider.controller;

import com.novax.ex.activity.open.api.IeoOrderApi;
import com.novax.ex.activity.open.model.query.OrderInnerQuery;
import com.novax.ex.activity.open.model.query.OrderQuery;
import com.novax.ex.activity.open.model.request.IeoOrderRequest;
import com.novax.ex.activity.open.model.request.IeoRecommendExtRequest;
import com.novax.ex.activity.open.model.request.IeoRecommendRequest;
import com.novax.ex.activity.open.model.response.IeoBuyRecommendExtResponse;
import com.novax.ex.activity.open.model.response.IeoBuyRecommendResponse;
import com.novax.ex.activity.open.model.response.IeoDistributionResponse;
import com.novax.ex.activity.open.model.response.IeoShareSubscribeResponse;
import com.novax.ex.activity.open.model.response.IeoSubscribeResponse;
import com.novax.ex.activity.provider.service.IeoDistributionService;
import com.novax.ex.activity.provider.service.IeoOrderService;
import com.novax.ex.common.constant.InternationalCode;
import com.novax.ex.common.results.ReturnResult;
import com.novax.ex.common.util.CopyUtils;
import com.novax.ex.common.util.PageUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RestController;

import jakarta.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * @Description 申购Controller
 * @Author Hsylient
 * @Date 2022/12/05/13:53
 */
@Slf4j
@RestController
public class IeoOrderController extends BaseActivityController implements IeoOrderApi {

    @Resource
    private IeoOrderService ieoOrderService;

    @Override
    public ReturnResult<PageUtil<IeoSubscribeResponse>> page(Long userId, OrderQuery query) {
        OrderInnerQuery innerQuery = CopyUtils.copyObject(query, OrderInnerQuery.class);
        ArrayList<Long> userIdsList = new ArrayList<>();
        userIdsList.add(userId);
        innerQuery.setUserIdList(userIdsList);
        PageUtil<IeoSubscribeResponse> pageInfo = new PageUtil<>(innerQuery);
        pageInfo.setCondition(Collections.singletonMap("query", innerQuery));
        ieoOrderService.findPage(pageInfo, IeoSubscribeResponse.class);
        // 未发币前 得到数量, 认购状态, 实际支付数量 不展示
        List<IeoSubscribeResponse> items = pageInfo.getItems();
        items.forEach(s -> {
            if(s.getUnfreeze() < 2){
                s.setAmount("--");
                s.setState(0);
                s.setRealPayAmount("");
            }
        });
        return ReturnResult.success(pageInfo);
    }

    @Override
    public ReturnResult<?> add(Long userId, String language, String uri, IeoOrderRequest orderRequest) {
        orderRequest.setCurrency(currencyToLowerCase(orderRequest.getCurrency()));
        orderRequest.setPayCurrency(currencyToLowerCase(orderRequest.getPayCurrency()));
        ReturnResult<?> returnResult = ieoOrderService.addOrder(userId, language, uri, orderRequest);
        return returnResult;
    }

    @Override
    public ReturnResult<IeoBuyRecommendResponse> recommend(Long userId, String language, IeoRecommendRequest ieoRecommendRequest) {
        return ieoOrderService.recommend(userId, language, ieoRecommendRequest);
    }
}
